/**
 * 
 */
package gui;

import gui.views.TypeInspector;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionListener;

import metagrammar.Type;
import application.Application;
import application.GlobalContext;

import com.google.common.collect.ImmutableList;

/**
 * @author Joshua Lockerman
 *
 */
public class TypeListWidget extends JPanel {
    
    private final GlobalContext context;
    
    private JList<metagrammar.Type> typesList;
    
    private DefaultListModel<Type> allTypesModel;
    private JComboBox<DefaultListModel<metagrammar.Type>> librarySelector;
    
    private BoxLayout layout;
    
    /**
     * 
     */
    public TypeListWidget()
    {
        context = Application.injector.getInstance(GlobalContext.class);
        
        layout = new BoxLayout(this, BoxLayout.X_AXIS);
        setLayout(layout);
        
        typesList = new JList<metagrammar.Type>();
        typesList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                if(e.getButton() == MouseEvent.BUTTON3) {
                    int librayIndex = librarySelector.getSelectedIndex();
                    int listIndex = typesList.getSelectedIndex();
                    TypeInspector ins = new TypeInspector(librayIndex, listIndex);
                    ins.setVisible(true);
                }
            }
        });
        
        final DefaultComboBoxModel<DefaultListModel<metagrammar.Type>> librarySelectorModel = new DefaultComboBoxModel<DefaultListModel<metagrammar.Type>>();
        librarySelector = new JComboBox<DefaultListModel<metagrammar.Type>>(librarySelectorModel);
        
        DefaultListModel<metagrammar.Type> knownTypeModel = new DefaultListModel<metagrammar.Type>() {
            @Override
            public String toString()
            {
                return "Loaded Types";
            }
        };
        ImmutableList<Type> knownTypes = context.getKnownTypes();
        for(Type t : knownTypes) {
            knownTypeModel.addElement(t);
        }
        typesList.setModel(knownTypeModel);
        librarySelectorModel.addElement(knownTypeModel);
        
        allTypesModel = new DefaultListModel<metagrammar.Type>() {
            @Override
            public String toString()
            {
                return "All Types";
            }
        };
        librarySelectorModel.addElement(allTypesModel);
        
        JScrollPane sp = new JScrollPane(typesList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(sp);
        
        librarySelector.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                int index = librarySelector.getSelectedIndex();
                DefaultListModel<metagrammar.Type> newModel = librarySelector.getItemAt(index);
                if(newModel == allTypesModel && newModel.isEmpty()) {
                    Collection<metagrammar.Type> types = context.tempGetAllTypes();
                    for(Type t : types) {
                        newModel.addElement(t);
                    }
                }
                typesList.setModel(newModel);
                if(newModel.isEmpty()) {
                    typesList.setPrototypeCellValue(metagrammar.Type.UNTYPED);
                }
                else {
                    typesList.setPrototypeCellValue(null);
                }
            }
        });
        
        setBorder(BorderFactory.createLoweredSoftBevelBorder());
        
        librarySelector.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLoweredSoftBevelBorder(),
                BorderFactory.createTitledBorder("Library")));
        
        JPanel panel = new JPanel();
        panel.add(librarySelector);
        
        librarySelector.setSelectedIndex(0);
        
        add(panel);
    }
    
    public void addListSelectionListener(ListSelectionListener listener)
    {
        typesList.addListSelectionListener(listener);
    }
    
    public void addMouseListenerToList(MouseListener listener)
    {
        typesList.addMouseListener(listener);
    }
    
    public Type getSelectedType()
    {
        return typesList.getSelectedValue();
    }
    
    public void setSelection(int libraryIndex, int typeIndex)
    {
        librarySelector.setSelectedIndex(libraryIndex);
        typesList.setSelectedIndex(typeIndex);
    }
}
